<?php

/**
 * fiche actions.
 *
 * @package    s-test
 * @subpackage fiche
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ficheActions extends sfActions
{
  public function executeKnockout(sfWebRequest $request){

  }
  public function executeIndex(sfWebRequest $request)
  {
    if($request->isXmlHttpRequest()){
        $arr = array();
        
        if($request->getParameter("cat") == "Tous")
            $this->fiches = KTDCharacterFiche::getOrderedFiches();
        else if($request->getParameter("cat") == "Non-valides")
            $this->fiches = KTDCharacterFiche::getInvalidFiches();
        else if($request->getParameter("cat") == "Valides")
            $this->fiches = KTDCharacterFiche::getValidFiches();
        else
            $this->fiches = KTDCharacterFiche::getOrderedFiches();

        foreach($this->fiches as $fiche){
            $arr[] = array(
                'character_pseudo'  => $fiche->getKTDCharacter()->getPseudo(),
                'fiche_link'        => $this->generateUrl('show_fiche', $fiche->getKTDCharacter()),
                
                'player_pseudo'     => $fiche->getKTDCharacter()->getKTDPlayer()->getPseudo(),
                'player_link'       => $this->generateUrl('show_player', $fiche->getKTDCharacter()->getKTDPlayer()),

                'created_at'        => $fiche->getCreatedAt(),

                'fiche_state_label' => $fiche->getKTDCharacter()->getIsValid() ? 'Validé !': "Invalide",
                'fiche_state'       => $fiche->getKTDCharacter()->getIsValid() ? 'is-valid': "is-invalid",

                'update_state_label'=> $fiche->getKTDCharacter()->hasUpdate() ? 'Il y a du nouveau !': "Rien de neuf",
                'update_state'      => $fiche->getKTDCharacter()->hasUpdate() ? 'is-new': "is-old",

                
            );
        }
        
        $json = json_encode($arr);
        $this->getResponse()->setHttpHeader('Content-type', 'application/json');
        return $this->renderText($json);
    }
    $this->ktd_character_fiches = KTDCharacterFiche::getOrderedFiches();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->ktd_character_fiche = Doctrine::getTable('KTDCharacterFiche')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->ktd_character_fiche, sprintf("La fiche n°%s est introuvable.", $request->getParameter('id')));
    $this->ktd_character_fiche->getKTDCharacter()->setOld();

  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new KTDCharacterFicheForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new KTDCharacterFicheForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($ktd_character_fiche = Doctrine::getTable('KTDCharacterFiche')->find(array($request->getParameter('id'))), sprintf('Object ktd_character_fiche does not exist (%s).', $request->getParameter('id')));
    $this->forward404Unless($this->getUser()->hasRelPermission("edit_fiche", $ktd_character_fiche->getKTDCharacter()->getKTDPlayer()), "Vous ne pouvez pas éditer cette fiche !");


    $this->form = new KTDCharacterFicheForm($ktd_character_fiche);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($ktd_character_fiche = Doctrine::getTable('KTDCharacterFiche')->find(array($request->getParameter('id'))), sprintf('Object ktd_character_fiche does not exist (%s).', $request->getParameter('id')));
    $this->form = new KTDCharacterFicheForm($ktd_character_fiche);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($ktd_character_fiche = Doctrine::getTable('KTDCharacterFiche')->find(array($request->getParameter('id'))), sprintf('Object ktd_character_fiche does not exist (%s).', $request->getParameter('id')));
    $ktd_character_fiche->delete();

    $this->redirect('fiche/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $ktd_character_fiche = $form->save();
      $this->redirect($this->generateUrl("show_fiche", $ktd_character_fiche->getKTDCharacter()));
    }
  }
}
